<?php

namespace App\Http\Middleware;

use App\Models\Admin\AdminUser;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;

class RouteAuth
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        $route = Route::currentRouteName();
        //_attach
        $mark = preg_replace ("/.attach(.[\d]+){0,1}$/", "",$route);
        if(!Auth::user()->hasPermissionTo($mark)){
            return response()->view('admin.errors.403', ['status' => "无访问权限"]);
        }

        return $next($request);
    }
}
